//
//  NSDictionary+ILGLib.m
//  ILGDemo
//
//  Created by happy on 2020/2/23.
//  Copyright © 2020 Happy. All rights reserved.
//

#import "NSDictionary+ILGLib.h"

@implementation NSDictionary (ILGLib)
// 通过这个方法，自动将字典转成模型中需要用的属性代码

// 私有API:真实存在,但是苹果没有暴露出来,不给你。如BOOL值，不知道类型，打印得知是__NSCFBoolean，但是无法敲出来，只能用NSClassFromString(@"__NSCFBoolean")

// isKindOfClass：判断下是否是当前类或者子类，BOOL是NSNumber的子类，要先判断BOOL
- (void)ilg_createPropetyCode
{
    // 模型中属性根据字典的key
    // 有多少个key,生成多少个属性
    NSMutableString *codes = [NSMutableString string];
    // 遍历字典
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull value, BOOL * _Nonnull stop) {
        NSString *code = nil;
        
        if ([value isKindOfClass:[NSString class]]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;",key];
        } else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",key];
        } else if ([value isKindOfClass:[NSNumber class]]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@;",key];
        } else if ([value isKindOfClass:[NSArray class]]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",key];
        } else if ([value isKindOfClass:[NSDictionary class]]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;",key];
        }
        
        // 拼接字符串
        [codes appendFormat:@"%@\n",code];
        
    }];
    
    NSLog(@"%@",codes);
}

@end
